package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.Advert;
import com.ybkj.daijia.server.service.AdvertService;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "advert")
public class AdvertController {

    @Autowired
    private AdvertService advertService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list() {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("advert")) {
            ModelAndView mv = new ModelAndView();

            mv.setViewName("advert/list");
            mv.addObject("page", advertService.queryAll());

            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView add() {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("advert")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("advert/add");

            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(Advert advert, Model model) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("advert")) {

            int record = advertService.findAll().size();

            if (record < 5) {

                advertService.save(advert);
            } else {
                model.addAttribute("message", "最多只能上传五张图片");
                return "advert/add";
            }

            return "redirect:list";
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("advert")) {
            ModelAndView mv = new ModelAndView();

            Advert advert = advertService.findById(id);

            mv.addObject("advert", advert);
            mv.setViewName("advert/edit");

            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(Advert advert) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("advert")) {

            Advert a = advertService.findById(advert.getId());
            a.setAdName(advert.getAdName());
            a.setAddress(advert.getAddress());
            a.setPicture(advert.getPicture());

            advertService.updateByPrimaryKey(a);

            return "redirect:list";
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Result<Integer> delete(Long id, HttpServletRequest req) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("advert")) {
            Result<Integer> result = new Result<Integer>();

            if (null != id) {
                Advert advert = advertService.findById(id);
                if (null != advert) {

                    advertService.delete(advert);

                    result.setSuccess(true);
                    result.setMessage("删除成功");
                } else {
                    result.setMessage("没有可以删除的数据");
                }
            } else {
                result.setMessage("没有可以删除的数据");
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

}
